<?php
declare(strict_types=1);

namespace App\Controller\admin;

use App\Controller\AbstractController;
use App\Request\Admin\UpdateRoleRequest;
use App\Request\Admin\UpdateStatusRequest;
use App\Resource\Admin\SystemLogResource;
use App\Resource\Admin\UserResource;
use App\Service\Admin\SystemLogService;
use App\Service\Admin\UserService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PatchMapping;
use Hyperf\HttpServer\Contract\RequestInterface;

#[Controller(prefix: '/api/admin/users')]
class UserController extends AbstractController
{
    #[Inject]
    private UserService $userService;

    #[Inject]
    private SystemLogService $systemLogService;

    #[GetMapping('')]
    public function index(RequestInterface $request)
    {
        $params = $request->getQueryParams();

        $users = $this->userService->getUsers($params);

        return UserResource::collection($users);
    }

    #[GetMapping('{id}')]
    public function show(int $id)
    {
        $user = $this->userService->getUserDetail($id);

        return new UserResource($user);
    }

    #[PatchMapping('{id}/roles')]
    public function updateRole(int $id, UpdateRoleRequest $request)
    {
        $data = $request->validated();

        $user = $this->userService->updateRole($id, $data['role']);

        return new UserResource($user);
    }

    #[PatchMapping('{id}/statuses')]
    public function updateStatus(int $id, UpdateStatusRequest $request)
    {
        $data = $request->validated();

        $user = $this->userService->updateStatus($id, $data['status']);

        return new UserResource($user);
    }

    #[GetMapping('{id}/system-logs')]
    public function getSystemLogs(int $id, RequestInterface $request)
    {
        $perPage = $request->query('perPage', 15);
        $page = $request->query('page', 1);

        $logs = $this->systemLogService->getLogsByUser($id, $perPage, $page);

        return SystemLogResource::collection($logs);
    }
}